<?php
class Core_Helper_Default {

	/**
	 * Uppercase the first character of each word in a string, seperate by underscore
	 *
	 * @return string
	 */
	public function ucwords($str) {
		$result = str_replace('_', ' ', $str);
		$result = ucwords($result);
		$result = str_replace(' ', '_', $result);
		return $result;
	}
	
	/**
	 * Return the values from a single column in the input array
	 *
	 * @param   array   $input
	 * @param   string  $column
	 * @return array
	 */
	public function arrayColumn(array $input, $column) {
		$result = array();
		foreach ($input as $key => $row)
			$result[] = $row[$column];
		return $result;
	}
	
	/**
	 * Return a value from input array
	 *
	 * @param   array   &$input
	 * @param   mixed   $value
	 * @return array
	 */
	public function arrayRemove(array &$input, $value) {
		if(($key = array_search($value, $input)) !== false) {
			unset($input[$key]);
		}
	}

	/**
	 * Sort multi dismention array by column
	 *
	 * @param   array   $arr
	 * @param   string  $column
	 * @return  mixed
	 */
	public function sortTable(array &$arr, $column) {
		$sort = $this->arrayColumn($arr, $column);
		array_multisort($sort, SORT_ASC, $arr);
	}

	/**
	 * Search multi dismention array by column value
	 *
	 * @param   array   $arr
	 * @param   string  $column
	 * @param   string  $value
	 * @return  array
	 */
	public function searchTable($arr, $column, $value) {
		$result = array();
		foreach($arr as $row) {
			if ($row[$column] == $value)
				$result[] = $row;
		}
		return $result;
	}
	
	/**
	 * Append node to simplexml element
	 *
	 * @param   SimpleXMLElement   $dst
	 * @param   SimpleXMLElement   $src
	 */
	public function appendXml(SimpleXMLElement $dst, SimpleXMLElement $src) {
		$dstDom = dom_import_simplexml($dst);
		$srcDom = dom_import_simplexml($src);
		$dstDom->appendChild($dstDom->ownerDocument->importNode($srcDom, true));
	}
	
	/**
	 * Get attribute value of xml node 
	 *
	 * @param   SimpleXMLElement   $node
	 * @param   string             $atr
	 * @return  string
	 */
	public function getNodeAttribute(SimpleXMLElement $node, $atr) {
		return (string)$node->attributes()->$atr;
	}

}